package com.berchen.msm;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.HashMap;
import java.util.Map;

/**
 * 发送单条短信
 */
public class SendMsm {
    public static void main(String[] args) {
        // 整合阿里云的短信服务
// 设置相关参数
        DefaultProfile profile = DefaultProfile.
                getProfile("default",
                        "LTAI5tDanP2GfVz2psAvCSwn",
                        "pyvejdxWyQPx1N0yIKqVt68psovBNb");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        //手机号
        request.putQueryParameter("PhoneNumbers", "15208366072");
        //签名名称
        request.putQueryParameter("SignName", "富顿科技");
        //模板code
        request.putQueryParameter("TemplateCode", "SMS_229477362");
        Map<String,Object> param = new HashMap();
        param.put("name","15208366072");
        param.put("count",1235);

        System.out.println(JSONObject.toJSONString(param));
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            System.out.println(response.getHttpResponse().isSuccess());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
